<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    利用Guzzle刷豆瓣热评引发的联想 |
    
    无敌小笼包</title>
  
    <link rel="shortcut icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
  
  <script src="/js/pace.min.js"></script>
</head>

<body>
<main class="content">
  <section class="outer">
  <article id="post-Guzzle" class="article article-type-post" itemscope itemprop="blogPost">

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      利用Guzzle刷豆瓣热评引发的联想
    </h1>
  

      </header>
    

    
      <div class="article-meta">
        <a href="/2017/04/12/Guzzle/" class="article-date">
  <time datetime="2017-04-12T04:47:24.000Z" itemprop="datePublished">2017-04-12</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/php/">php</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      
      
      
        <h4 id="Guzzle是一个PHP的HTTP客户端，用来轻而易举地发送请求，并集成到我们的WEB服务上。"><a href="#Guzzle是一个PHP的HTTP客户端，用来轻而易举地发送请求，并集成到我们的WEB服务上。" class="headerlink" title="Guzzle是一个PHP的HTTP客户端，用来轻而易举地发送请求，并集成到我们的WEB服务上。"></a>Guzzle是一个PHP的HTTP客户端，用来轻而易举地发送请求，并集成到我们的WEB服务上。</h4><ul>
<li>接口简单：构建查询语句、POST请求、分流上传下载大文件、使用HTTP cookies、上传JSON数据等等 。</li>
<li>发送同步或异步的请求均使用相同的接口。</li>
<li>使用PSR-7接口来请求、响应、分流，允许你使用其他兼容的PSR-7类库与Guzzle共同开发。</li>
<li>抽象了底层的HTTP传输，允许你改变环境以及其他的代码，如：对cURL与PHP的流或socket并非重度依赖，非阻塞事件循环。</li>
<li>中间件系统允许你创建构成客户端行为。</li>
</ul>
<a id="more"></a>
<h2 id="所需包"><a href="#所需包" class="headerlink" title="所需包"></a>所需包</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&quot;require&quot;: &#123;</div><div class="line">	&quot;guzzlehttp/guzzle&quot;: &quot;6.2.*&quot;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="源码"><a href="#源码" class="headerlink" title="源码"></a>源码</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">include_once dirname(__FILE__).&apos;/vendor/autoload.php&apos;;</div><div class="line"></div><div class="line">use \GuzzleHttp\Client;</div><div class="line">use \GuzzleHttp\Cookie\CookieJar;</div><div class="line">use \GuzzleHttp\Exception\RequestException;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 豆瓣租房刷留言</div><div class="line"> */</div><div class="line"></div><div class="line">class DouBanBrush&#123;</div><div class="line">    public $jar;</div><div class="line">    public $clock = 10;</div><div class="line">    const SLEEP   = 1200;   // 睡20 分钟</div><div class="line"></div><div class="line">    /**</div><div class="line">     * [__autoload 初始化]</div><div class="line">     * @author     Shaowei Pu &lt;542684913@qq.com&gt;</div><div class="line">     * @CreateTime  2017-04-12T10:39:28+0800</div><div class="line">     * @param                               [type] $account  [description]</div><div class="line">     * @param                               [type] $password [description]</div><div class="line">     * @return                              [type]           [description]</div><div class="line">     */</div><div class="line">    public function __construct( $account, $password )&#123;</div><div class="line">        $this-&gt;jar = new CookieJar;</div><div class="line">        try&#123;</div><div class="line">            if( $this-&gt;login( $account, $password ) == &apos;200&apos;)&#123;</div><div class="line">                echo &quot;----------【 START 】----------\n&quot;;</div><div class="line">                    $this-&gt;send();</div><div class="line">                echo &quot;----------【  END  】----------\n&quot;;</div><div class="line">            &#125;else&#123;</div><div class="line">              echo &quot;登录失败～！&quot;;</div><div class="line">            &#125;</div><div class="line">        &#125;catch (RequestException $e) &#123;</div><div class="line">            var_dump( $e-&gt;getRequest());</div><div class="line">            if ($e-&gt;hasResponse()) &#123;</div><div class="line">                var_dump( $e-&gt;getResponse());</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    /**</div><div class="line">     * [login 登录]</div><div class="line">     * @author     Shaowei Pu &lt;542684913&gt;</div><div class="line">     * @CreateTime  2017-04-12T10:42:16+0800</div><div class="line">     * @return                              [type] [description]</div><div class="line">     */</div><div class="line">    public function login( $account , $password )&#123;</div><div class="line">      // 清楚空间内cookie</div><div class="line">      // $this-&gt;jar-&gt;clear();</div><div class="line">      return ( new Client([ &apos;cookies&apos;  =&gt;  true ]) )-&gt;request(</div><div class="line">            &apos;POST&apos;, </div><div class="line">            &apos;https://accounts.douban.com/j/popup/login/basic&apos;,</div><div class="line">            [</div><div class="line">              &apos;version&apos; =&gt; 1.1 ,</div><div class="line">              &apos;cookies&apos;         =&gt; $this-&gt;jar,</div><div class="line">              &apos;headers&apos;         =&gt; [</div><div class="line">                                  &apos;Accept&apos;       =&gt; &apos;application/json&apos;,</div><div class="line">                                  &apos;Referer&apos;      =&gt; &apos;登录来源页&apos;</div><div class="line">              ],</div><div class="line">              &apos;form_params&apos;     =&gt; [</div><div class="line">                                &apos;source&apos;           =&gt; &apos;group&apos;,</div><div class="line">                                &apos;referer&apos;          =&gt; &apos;提交接口&apos;,</div><div class="line">                                &apos;name&apos;             =&gt; $account,</div><div class="line">                                &apos;password&apos;         =&gt; $password,</div><div class="line">                                &apos;captcha_id&apos;       =&gt; &apos;&apos;,</div><div class="line">                                &apos;captcha_solution&apos; =&gt; &apos;&apos;</div><div class="line">                ]</div><div class="line">          ])-&gt;getStatusCode();</div><div class="line">    &#125;</div><div class="line">    /**</div><div class="line">     * [send 发送内容]</div><div class="line">     * @author     Shaowei Pu &lt;542684913@qq.com&gt;</div><div class="line">     * @CreateTime  2017-04-12T10:43:17+0800</div><div class="line">     * @return                              [type] [description]</div><div class="line">     */</div><div class="line">    public function send() &#123;</div><div class="line">        // 获得 ck</div><div class="line">        $this-&gt;reload();        </div><div class="line">        $ck    = &apos;ntxB&apos;;</div><div class="line">        array_map(function( $val ) use  (&amp; $ck )&#123;  $val[&apos;Name&apos;] == &apos;ck&apos; &amp;&amp; $ck = $val[&apos;Value&apos;]; &#125;, $this-&gt;jar-&gt;toArray());</div><div class="line">        // 计时器</div><div class="line">        while ( $this-&gt;clock &gt; 0 ) &#123;</div><div class="line">          $send_content =  ( new Client([ &apos;cookies&apos;  =&gt;  true ]) )-&gt;request(</div><div class="line">            &apos;POST&apos;, </div><div class="line">            &apos;提交接口&apos;,</div><div class="line">            [</div><div class="line">              &apos;version&apos;         =&gt; 1.1,</div><div class="line">              &apos;cookies&apos;         =&gt; $this-&gt;jar,</div><div class="line">              &apos;headers&apos;         =&gt; [</div><div class="line">                                  &apos;Accept&apos;       =&gt; &apos;application/json&apos;,</div><div class="line">                                  &apos;Referer&apos;      =&gt; &apos;来源页&apos;</div><div class="line">              ],</div><div class="line">              &apos;form_params&apos;     =&gt; [</div><div class="line">                                &apos;ck&apos;                =&gt; $ck,</div><div class="line">                                &apos;rv_comment&apos;        =&gt; &apos;自己顶一下～！&apos;,</div><div class="line">                                &apos;start&apos;             =&gt; 0,</div><div class="line">                                &apos;submit_btn&apos;        =&gt;&apos;加上去&apos;</div><div class="line">                                ]</div><div class="line">              ])-&gt;getBody()-&gt;getContents();</div><div class="line">              echo date(&apos;Y-m-d H:i:s&apos;).&apos; &apos;.$this-&gt;clock.&quot;\n&quot;;</div><div class="line">              sleep( self::SLEEP );</div><div class="line">              --$this-&gt;clock;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    /**</div><div class="line">     * [reload 刷新页面]</div><div class="line">     * @author     Shaowei Pu &lt;542684913@qq.com&gt;</div><div class="line">     * @CreateTime  2017-04-12T13:35:58+0800</div><div class="line">     * @return                              [type] [description]</div><div class="line">     */</div><div class="line">    public function reload()&#123;</div><div class="line">      (new Client([ &apos;cookies&apos;  =&gt;  true ])) -&gt;request(&apos;GET&apos;, &apos;访问页&apos;,[</div><div class="line">          &apos;cookies&apos;         =&gt; $this-&gt;jar,</div><div class="line">          &apos;headers&apos;         =&gt; [</div><div class="line">                &apos;Accept&apos;       =&gt; &apos;application/json&apos;,</div><div class="line">                &apos;Referer&apos;      =&gt; &apos;来源页&apos;</div><div class="line">       ]]);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">new DouBanBrush(&apos;账号&apos;,&apos;密码&apos;);</div></pre></td></tr></table></figure>
<h2 id="还没做的事"><a href="#还没做的事" class="headerlink" title="还没做的事"></a>还没做的事</h2><ul>
<li>验证码识别</li>
<li>优化效率</li>
</ul>
<h2 id="致敬"><a href="#致敬" class="headerlink" title="致敬"></a>致敬</h2><p>像 @娃娃脾气 大佬致敬</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://blog.mango16.cc/2017/04/12/Guzzle/" data-id="cjrbtw1xq000b6d8hrswx4dw2"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/phper/">phper</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2017/05/31/learnLaravel/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            Laravel 简短学习 just write the code in the PHP way
          
        </div>
      </a>
    
    
      <a href="/2017/03/10/everyday/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">But you should have one thing mind.</div>
      </a>
    
  </nav>


  

  
    
  <div class="gitalk" id="gitalk-container"></div>
  <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
  <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
  <script type="text/javascript">
      var gitalk = new Gitalk({
        clientID: 'caba8826a61790936307',
        clientSecret: '09a15037dcab30d9adf35a53ec6961939169f797',
        repo: 'm9rco.github.io',
        owner: 'm9rco',
        admin: ['m9rco'],
        // id: location.pathname,      // Ensure uniqueness and length less than 50
        id: md5(location.pathname),
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

  gitalk.render('gitalk-container')
  </script>

  

</article>



</section>
  <footer class="footer">
  
  <div class="outer">
    <ul class="list-inline">
      <li>&copy; 2019 无敌小笼包</li>
      <i class="pulse"></i>
    </ul>
  </div>
</footer>

</main>
<aside class="sidebar">
  <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/hexo.svg" alt="无敌小笼包"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">相册</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/nagging">叽咕</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="Search">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
</aside>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

</body>
</html>